﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web.UI.HtmlControls;

namespace TotalSport
{
    public partial class viewallproducts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["TotalSport"].ConnectionString;
            if (Session["UserId"] == null || Session["UserId"].ToString() == "")
            {
                Session.Abandon();
                Session.RemoveAll();
                Response.Redirect("LoginUser.aspx");
            }          
            if (!IsPostBack)
            {
                getViewPRoducts();            
            }
        }
        protected void grdQuotes_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {           
            grdProducts.PageIndex = e.NewPageIndex;
            getViewPRoducts();
        }
        private void getViewPRoducts()
        {
            DataSet ds = BAL.Customers.getallProducts();
            DataTable dt = new DataTable();           
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    grdProducts.DataSource = ds;
                    grdProducts.DataBind();
                }
                else
                    grdProducts.DataBind();
            }
        }

        protected void lnkDownload_Click(object sender, EventArgs e)
        {
            //PrepareForExport(grdProducts);
            //ExportToExcel();         
            Response.Redirect("DownloadProducts.aspx");
        }
        private void PrepareForExport(Control ctrl)
        {
            //iterate through all the grid controls
            foreach (Control childControl in ctrl.Controls)
            {
                //if the control type is link button, remove it
                //from the collection
                if (childControl.GetType() == typeof(LinkButton))
                {
                    ctrl.Controls.Remove(childControl);
                }
                //if the child control is not empty, repeat the process
                // for all its controls
                else if (childControl.HasControls())
                {
                    PrepareForExport(childControl);
                }
            }
        }
        private void ExportToExcel()
        {
            Response.Clear();
            Response.AddHeader("content-disposition",
                                  "attachment;filename=Strucutreproduct.xls");        
            Response.ContentType = "application/ms-excel";
            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(stringWriter);
            grdProducts.AllowPaging = false;
            grdProducts.RenderControl(HtmlTextWriter);
            Response.Write(stringWriter.ToString());
            Response.Flush();
            Response.End();
        }
        public override void VerifyRenderingInServerForm(Control control)
        { }

        protected void lnkUpload_Click(object sender, EventArgs e)
        {

        }
      
        
    }
}